const allSqlAction = require('../lib/mysql')
const {user,role} = require('../model/index');
module.exports = {
    findUserByPhone:async(phone)=>{
        let sql = `select * from user1 where phone = ${phone}`;
        return allSqlAction.allSqlAction(sql).then(res=>{
            if(res.length>=1){
                return{
                    userid:res[0].id,
                    username:res[0].username,
                    password:res[0].password,
                    phone:res[0].phone
                }
            }else{
                return null;
            }
        })
    },
    getUserByPhone:async(phone)=>{
        return await user.findOne({
            where:{
                phone
            }
        })
    },
    getUserBySearchInfo:async(query)=>{
        try{
            let result = await user.findAll({
                where:query,
                attributes:["id","username","phone","roleid"],
                include:[role]
            });
            return result;

        }catch(error){
            console.log("根据条件查询用户出错："+error);
            return null;
        }
    },
    createUser:async(data)=>{
        try{
            let result=await user.create(data);
            return result;
        }catch(error){
            console.log("新增用户出错："+error);
            return null;
        }
    },
    getAllRoles:async()=>{
        try{
            let result = await role.findAll({
                
              
            });
            return result;
        }catch(error){
            console.log("获取全部角色错误："+error);
            return null;
        }
    }
}